<?php
/**
 * This class allow store error message or warning message
 * @author HoanVX
 * @version $Id: validation.php 72 2015-01-18 03:03:16Z voxuanhoan@gmail.com $
 *
 */
class Validation {

	private $error;
	private $message;

	function __construct() {
		$this->error = array();
		$this->message = array();
	}

	/**
	 * Put error in list
	 * @param unknown $itemName: item which have error validation
	 * @param string $message
	 */
	public function setError($args = array()) {
		$default = array(
				'containner'		=>	'label',
				'containnerClass'	=> 'error',
				'itemName'			=> '',
				'message'			=> '',
				'for'				=> ''
		);

		$args = wp_parse_args($args, $default);

		// If $args['for'] is empty then i set default is item name
		$args['for'] = empty($args['for']) ? $args['itemName'] : $args['for'];

		if (empty($args['itemName']))
			error_log(LOG_CHOTROI. $args['itemName']. ": is not set");

		$tmp = array();

		if (!empty($this->error) && array_key_exists($args['itemName'], $this->error)) {
			$tmp = $this->error[$args['itemName']];
		}

		$tag = "";
		if (strcmp($args['containner'], 'label') == 0) {
			$tag = "<label for='{$args['for']}' class='{$args['containnerClass']}'> {$args['message']} </label>";
		} else {
			$tag = "<{$args['containner']} class='{$args['containnerClass']}'> {$args['message']} </{$args['containner']}>";
		}
		array_push($tmp, $tag);
		$this->error[$args['itemName']] = $tmp;
	}

	/**
	 * Put message in list. maybe warning message
	 * @param unknown $itemName: item which have error validation
	 * @param string $message
	 */
	public function setMessage($itemName, $message='', $containClass='message') {
		if (!isset($itemName))
			error_log($itemName.": is not set");

		$tmp = array();
		if (!empty($this->message) && array_key_exists($itemName, $this->message)) {
			$tmp = $this->message[$itemName];
		}

		array_push($tmp, "<div class='". $containClass ."'>". $message . "</div>");
		$this->message[$itemName] = $tmp;
	}

	/**
	 * Get all error of item's message
	 * @param unknown $itemName: key will determine error message
	 */
	public function getError($itemName) {
		$errorMessage = "";

		if (!empty($this->error) && array_key_exists($itemName, $this->error)) {
			foreach ($this->error[$itemName] as $val) {
				$errorMessage .= $val;
			}
		}

		return $errorMessage;
	}

	/**
	 * Get all message of item's message
	 * @param unknown $itemName: key will determine error message
	 */
	public function getMessage($itemName) {
		$message = "";

		if (!empty($this->message) && array_key_exists($itemName, $this->message)) {
			foreach ($this->message[$itemName] as $val) {
				$message .= $val;
			}
		}

		return $message;
	}

	/**
	 * Get all message of all items
	 */
	public function getMessages() {
		$message = "";

		if (!empty($this->message)) {
			foreach ($this->message as $key => $val) {
				$message .= implode("<br />", $val);
			}
		}

		return $message;
	}

	/**
	 * Check if current validation action has error
	 * return true if has error
	 * return false if hasn't error
	 */
	public function hasError() {
		if (!empty($this->error))
			return true;
		return false;
	}

	/**
	 * Check if current validation action has message
	 * return true if has message
	 * return false if hasn't message
	 */
	public function hasMessage() {
		if (!empty($this->message))
			return true;
		return false;
	}
}